package com.example.demo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import java.net.URL;
import java.util.Objects;


public class HelloApplicationHTML extends Application {
    @Override
    public void start(Stage stage) {
        //加载处理
        ProgressIndicator progressIndicator = new ProgressIndicator();
        progressIndicator.setMaxWidth(50);
        progressIndicator.setMaxHeight(50);
        StackPane initialStackPane = new StackPane(progressIndicator);
        //网页处理
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        Worker<Void> loadWorker = webEngine.getLoadWorker();
        ReadOnlyObjectProperty<Worker.State> stateReadOnlyObjectProperty = loadWorker.stateProperty();
        StackPane webViewStackPane= new StackPane(webView);
        Platform.runLater(() -> webViewStackPane.setVisible(false));
        //网页监听
        stateReadOnlyObjectProperty.addListener((observable, oldValue, newValue) -> {
            if(Objects.equals(newValue,Worker.State.SUCCEEDED)){
                Platform.runLater(() -> initialStackPane.setVisible(false));
                Platform.runLater(() -> webViewStackPane.setVisible(true));
                System.out.println("网页页面加载成功。");
            }
        });
        //加载全部StackPane
        StackPane stackPane = new StackPane(initialStackPane,webViewStackPane);
        Scene scene = new Scene(stackPane, 800, 600);
        stage.setTitle("测试"); //设置标题
        stage.setScene(scene);
        stage.show();
        //UI处理
        Platform.runLater(() -> {
            System.out.println("UI已就绪,执行其他操作。");
            initialStackPane.setVisible(true);
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
